package com.nantai.jiafang.nantaijiafang.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nantai.jiafang.nantaijiafang.IConstant;
import com.nantai.jiafang.nantaijiafang.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by wuning on 2016/8/23.
 */
public class SearchDateAdapter extends BaseAdapter {

    private JSONArray jsonArray;
    private Context mContext;
    private int list_type;
    private boolean isList1 = true;

    public SearchDateAdapter(Context context, JSONArray ja, int type) {
        this.jsonArray = ja;
        this.mContext = context;
        list_type = type;
    }

    public void setIsList1(boolean is1) {
        isList1 = is1;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return jsonArray.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return jsonArray.get(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        JSONObject jo = new JSONObject();
        try {
            jo = (JSONObject) jsonArray.get(position);
        } catch (JSONException e) {

        }
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_null_searchdata, null);
            viewHolder.ll1 = (LinearLayout) convertView.findViewById(R.id.ll_1);
            viewHolder.ll2 = (LinearLayout) convertView.findViewById(R.id.ll_2);
            viewHolder.ll3 = (LinearLayout) convertView.findViewById(R.id.ll_3);
            viewHolder.ll4 = (LinearLayout) convertView.findViewById(R.id.ll_4);
            viewHolder.ll5 = (LinearLayout) convertView.findViewById(R.id.ll_5);
            viewHolder.ll6 = (LinearLayout) convertView.findViewById(R.id.ll_6);
            viewHolder.ll7 = (LinearLayout) convertView.findViewById(R.id.ll_7);
            viewHolder.ll8 = (LinearLayout) convertView.findViewById(R.id.ll_8);
            viewHolder.ll9 = (LinearLayout) convertView.findViewById(R.id.ll_9);
            viewHolder.ll10 = (LinearLayout) convertView.findViewById(R.id.ll_10);

            viewHolder.name1 = (TextView) convertView.findViewById(R.id.tv_name1);
            viewHolder.name2 = (TextView) convertView.findViewById(R.id.tv_name2);
            viewHolder.name3 = (TextView) convertView.findViewById(R.id.tv_name3);
            viewHolder.name4 = (TextView) convertView.findViewById(R.id.tv_name4);
            viewHolder.name5 = (TextView) convertView.findViewById(R.id.tv_name5);
            viewHolder.name6 = (TextView) convertView.findViewById(R.id.tv_name6);
            viewHolder.name7 = (TextView) convertView.findViewById(R.id.tv_name7);
            viewHolder.name8 = (TextView) convertView.findViewById(R.id.tv_name8);
            viewHolder.name9 = (TextView) convertView.findViewById(R.id.tv_name9);
            viewHolder.name10 = (TextView) convertView.findViewById(R.id.tv_name10);

            viewHolder.content1 = (TextView) convertView.findViewById(R.id.tv_content1);
            viewHolder.content2 = (TextView) convertView.findViewById(R.id.tv_content2);
            viewHolder.content3 = (TextView) convertView.findViewById(R.id.tv_content3);
            viewHolder.content4 = (TextView) convertView.findViewById(R.id.tv_content4);
            viewHolder.content5 = (TextView) convertView.findViewById(R.id.tv_content5);
            viewHolder.content6 = (TextView) convertView.findViewById(R.id.tv_content6);
            viewHolder.content7 = (TextView) convertView.findViewById(R.id.tv_content7);
            viewHolder.content8 = (TextView) convertView.findViewById(R.id.tv_content8);
            viewHolder.content9 = (TextView) convertView.findViewById(R.id.tv_content9);
            viewHolder.content10 = (TextView) convertView.findViewById(R.id.tv_content10);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        switch (list_type) {
            //生产订单角度
            case IConstant.INTENT_PRODUCTION_ORDER_ATTITUDE:
                if (isList1) {
                    viewHolder.ll9.setVisibility(View.GONE);
                    viewHolder.ll10.setVisibility(View.GONE);
                    viewHolder.ll8.setVisibility(View.GONE);

                    viewHolder.name1.setText("生产订单号");
                    viewHolder.name2.setText("成品名称");
                    viewHolder.name3.setText("款号");
                    viewHolder.name4.setText("等级");
                    viewHolder.name5.setText("裁剪用数");
                    viewHolder.name6.setText("领用米数");
                    viewHolder.name7.setText("综合利用率");

                    viewHolder.content1.setText(jo.optString("生产订单号"));
                    viewHolder.content2.setText(jo.optString("成品名称"));
                    viewHolder.content3.setText(jo.optString("款号"));
                    viewHolder.content4.setText(jo.optString("等级"));
                    viewHolder.content5.setText(jo.optString("裁剪用数"));
                    viewHolder.content6.setText(jo.optString("领用米数"));
                    viewHolder.content7.setText(jo.optString("综合利用率"));
                } else {
                    viewHolder.ll10.setVisibility(View.GONE);
                    viewHolder.name1.setText("生产订单号");
                    viewHolder.name2.setText("成品名称");
                    viewHolder.name3.setText("款号");
                    viewHolder.name4.setText("面料名称");
                    viewHolder.name5.setText("面料编码");
                    viewHolder.name6.setText("等级");
                    viewHolder.name7.setText("裁剪用数");
                    viewHolder.name8.setText("领用米数");
                    viewHolder.name9.setText("综合利用率");

                    viewHolder.content1.setText(jo.optString("生产订单号"));
                    viewHolder.content2.setText(jo.optString("成品名称"));
                    viewHolder.content3.setText(jo.optString("款号"));
                    viewHolder.content4.setText(jo.optString("面料名称"));
                    viewHolder.content5.setText(jo.optString("面料编码"));
                    viewHolder.content6.setText(jo.optString("等级"));
                    viewHolder.content7.setText(jo.optString("裁剪用数"));
                    viewHolder.content8.setText(jo.optString("领用米数"));
                    viewHolder.content9.setText(jo.optString("综合利用率"));
                }

                break;
            case IConstant.INTENT_FabricBatch_ATTITUDE:
                if (isList1) {
                    viewHolder.ll10.setVisibility(View.GONE);
                    viewHolder.ll9.setVisibility(View.GONE);
                    viewHolder.ll8.setVisibility(View.GONE);
                    viewHolder.ll7.setVisibility(View.GONE);

                    viewHolder.name1.setText("面料名称");
                    viewHolder.name2.setText("面料编码");
                    viewHolder.name3.setText("等级");
                    viewHolder.name4.setText("裁剪用数");
                    viewHolder.name5.setText("领用米数");
                    viewHolder.name6.setText("综合利用率");

                    viewHolder.content1.setText(jo.optString("面料名称"));
                    viewHolder.content2.setText(jo.optString("面料编码"));
                    viewHolder.content3.setText(jo.optString("等级"));
                    viewHolder.content4.setText(jo.optString("裁剪用数"));
                    viewHolder.content5.setText(jo.optString("领用米数"));
                    viewHolder.content6.setText(jo.optString("综合利用率"));
                } else {
                    viewHolder.ll10.setVisibility(View.GONE);
                    viewHolder.ll9.setVisibility(View.GONE);
                    viewHolder.ll8.setVisibility(View.GONE);

                    viewHolder.name1.setText("面料名称");
                    viewHolder.name2.setText("面料编码");
                    viewHolder.name3.setText("供应商");
                    viewHolder.name4.setText("等级");
                    viewHolder.name5.setText("裁剪用数");
                    viewHolder.name6.setText("领用米数");
                    viewHolder.name7.setText("综合利用率");

                    viewHolder.content1.setText(jo.optString("面料名称"));
                    viewHolder.content2.setText(jo.optString("面料编码"));
                    viewHolder.content3.setText(jo.optString("供应商"));
                    viewHolder.content4.setText(jo.optString("等级"));
                    viewHolder.content5.setText(jo.optString("裁剪用数"));
                    viewHolder.content6.setText(jo.optString("领用米数"));
                    viewHolder.content7.setText(jo.optString("综合利用率"));
                }


                break;
            case IConstant.INTENT_SUPPLIER_ATTITUDE:

                if (isList1) {
                    viewHolder.ll10.setVisibility(View.GONE);
                    viewHolder.ll9.setVisibility(View.GONE);
                    viewHolder.ll8.setVisibility(View.GONE);

                    viewHolder.name1.setText("供应商");
                    viewHolder.name2.setText("面料名称");
                    viewHolder.name3.setText("面料编码");
                    viewHolder.name4.setText("等级");
                    viewHolder.name5.setText("裁剪用数");
                    viewHolder.name6.setText("领用米数");
                    viewHolder.name7.setText("综合利用率");

                    viewHolder.content1.setText(jo.optString("供应商"));
                    viewHolder.content2.setText(jo.optString("面料名称"));
                    viewHolder.content3.setText(jo.optString("面料编码"));
                    viewHolder.content4.setText(jo.optString("等级"));
                    viewHolder.content5.setText(jo.optString("裁剪用数"));
                    viewHolder.content6.setText(jo.optString("领用米数"));
                    viewHolder.content7.setText(jo.optString("综合利用率"));
                } else {
                    viewHolder.ll10.setVisibility(View.GONE);
                    viewHolder.ll9.setVisibility(View.GONE);

                    viewHolder.name1.setText("供应商");
                    viewHolder.name2.setText("面料名称");
                    viewHolder.name3.setText("面料编码");
                    viewHolder.name4.setText("采购合同号");
                    viewHolder.name5.setText("等级");
                    viewHolder.name6.setText("裁剪用数");
                    viewHolder.name7.setText("领用米数");
                    viewHolder.name8.setText("综合利用率");

                    viewHolder.content1.setText(jo.optString("供应商"));
                    viewHolder.content2.setText(jo.optString("面料名称"));
                    viewHolder.content3.setText(jo.optString("面料编码"));
                    viewHolder.content4.setText(jo.optString("采购合同号"));
                    viewHolder.content5.setText(jo.optString("等级"));
                    viewHolder.content6.setText(jo.optString("裁剪用数"));
                    viewHolder.content7.setText(jo.optString("领用米数"));
                    viewHolder.content8.setText(jo.optString("综合利用率"));
                }


                break;
            case IConstant.INTENT_PRODUCTIONCOST_ATTITUDE:
                viewHolder.name1.setText("成品名称");
                viewHolder.name2.setText("款号");
                viewHolder.name3.setText("面料名称");
                viewHolder.name4.setText("面料编码");
                viewHolder.name5.setText("Bom单耗");
                viewHolder.name6.setText("综合利用率");
                viewHolder.name7.setText("等级");
                viewHolder.name8.setText("Bom应领米数");
                viewHolder.name9.setText("U8出库米数");
                viewHolder.name10.setText("Bom利用率");

                viewHolder.content1.setText(jo.optString("成品名称"));
                viewHolder.content2.setText(jo.optString("款号"));
                viewHolder.content3.setText(jo.optString("面料名称"));
                viewHolder.content4.setText(jo.optString("面料编码"));
                viewHolder.content5.setText(jo.optString("Bom单耗"));
                viewHolder.content6.setText(jo.optString("综合利用率"));
                viewHolder.content7.setText(jo.optString("等级"));
                viewHolder.content8.setText(jo.optString("Bom应领米数"));
                viewHolder.content9.setText(jo.optString("U8出库米数"));
                viewHolder.content10.setText(jo.optString("Bom利用率"));
                break;
            case IConstant.INTENT_PURCHASECONTRACT_ACCITUDE:
                viewHolder.ll9.setVisibility(View.GONE);
                viewHolder.ll10.setVisibility(View.GONE);

                viewHolder.name1.setText("采购合同号");
                viewHolder.name2.setText("面料名称");
                viewHolder.name3.setText("面料编码");
                viewHolder.name4.setText("等级");
                viewHolder.name5.setText("供应商");
                viewHolder.name6.setText("裁剪用数");
                viewHolder.name7.setText("领用米数");
                viewHolder.name8.setText("综合利用率");

                viewHolder.content1.setText(jo.optString("采购合同号"));
                viewHolder.content2.setText(jo.optString("面料名称"));
                viewHolder.content3.setText(jo.optString("面料编码"));
                viewHolder.content4.setText(jo.optString("等级"));
                viewHolder.content5.setText(jo.optString("供应商"));
                viewHolder.content6.setText(jo.optString("裁剪用数"));
                viewHolder.content7.setText(jo.optString("领用米数"));
                viewHolder.content8.setText(jo.optString("综合利用率"));

                break;
        }
        return convertView;
    }

    class ViewHolder {
        LinearLayout ll1, ll2, ll3, ll4, ll5, ll6, ll7, ll8, ll9, ll10;
        TextView name1, name2, name3, name4, name5, name6, name7, name8, name9, name10;
        TextView content1, content2, content3, content4, content5, content6, content7, content8, content9, content10;
    }

}
